﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Web.Routing;

namespace jQM
{
    public static class HtmlHelperActionLinkExtensionMethods
    {
        private static UrlHelper urlHelper;

        public static MvcHtmlString ActionButton(this HtmlHelper helper, string buttontext, string action, string controller)
        {
            string url = buildUrl(helper, action, controller);
            string btn = buildButtonBase(url, buttontext).ToString(TagRenderMode.Normal);
            return MvcHtmlString.Create(btn);
        }

        public static MvcHtmlString ActionButton(this HtmlHelper helper, string buttontext, string action, string controller, object routeValues)
        {
            string url = buildUrl(helper, action, controller, routeValues);
            string btn = buildButtonBase(url, buttontext).ToString(TagRenderMode.Normal);
            return MvcHtmlString.Create(btn);
        }

        public static MvcHtmlString ActionButton(this HtmlHelper helper, string buttontext, string action, string controller, ButtonIcon icon)
        {
            string url = buildUrl(helper, action, controller);
            TagBuilder btn = buildButtonBase(url, buttontext);
            btn.Attributes.Add("data-icon", icon.ToString());
            return MvcHtmlString.Create(btn.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString ActionButton(this HtmlHelper helper, string buttontext, string action, string controller, object routeValues, ButtonIcon icon)
        {
            string url = buildUrl(helper, action, controller, routeValues);
            TagBuilder btn = buildButtonBase(url, buttontext);
            btn.Attributes.Add("data-icon", icon.ToString());
            return MvcHtmlString.Create(btn.ToString(TagRenderMode.Normal));
        }

        private static string buildUrl(HtmlHelper helper, string action, string controller, object routeValues = null)
        {
            urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
            if(routeValues == null)
            {
                routeValues = new object();
            }

            string url = urlHelper.Action(action, controller, routeValues);
            return url;
        }

        private static TagBuilder buildButtonBase(string url, string buttontext)
        {
            TagBuilder tag = new TagBuilder("a");
            tag.Attributes.Add("href", url);
            tag.Attributes.Add("data-role", "button");
            tag.SetInnerText(buttontext);
            return tag;
        }
    }

    public enum ButtonIcon
    {
        //arrow-l,
        //arrow-r,
        delete,
        plus,
        minus,
        check,
        gear,
        refresh,
        forward,
        back,
        grid,
        star,
        alert,
        info,
        home,
        search
    }
}